﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace EditColumn.ConnectColumnBream
{
	public class ConnectColumnBreamInfo
	{
		public ConnectColumnBreamInfo GetNextConnectColumnBreamInfo()
		{
			return this.m_nextConnect;
		}

		public XYZ ConnectPoint
		{
			get
			{
				return this.m_pConnect;
			}
		}

		public Line[] TagLine
		{
			get
			{
				return this.m_arTagLine;
			}
		}

		public int SerialNumber
		{
			get
			{
				return this.m_intConnectPoint;
			}
		}

		public BreamInfo ConnectBreamInfo
		{
			get
			{
				return this.m_BreamInfo;
			}
		}

		public FamilyInstance ConnectColumn
		{
			get
			{
				return this.m_column;
			}
		}

		public DetailCurve[] ArrayDetailCurve
		{
			get
			{
				return this.m_arModelLine;
			}
			set
			{
				this.m_arModelLine = value;
			}
		}

		private ConnectColumnBreamInfo()
		{
		}

		public static ConnectColumnBreamInfo CreateConnectColumnBreamInfo(ExternalCommandData m_commandData, BreamInfo breamInfo, XYZ point, FamilyInstance colum)
		{
			if (m_commandData == null || breamInfo == null || point == null || colum == null)
			{
				return null;
			}
			ConnectColumnBreamInfo result;
			try
			{
				ConnectColumnBreamInfo connectColumnBreamInfo = new ConnectColumnBreamInfo();
				if (breamInfo.BreamLocation.GetEndPoint(0).DistanceTo(point) < breamInfo.BreamLocation.GetEndPoint(1).DistanceTo(point))
				{
					connectColumnBreamInfo.m_intConnectPoint = 0;
					connectColumnBreamInfo.m_arTagLine = ConnectColumnBreamInfo.GettagLine(breamInfo, breamInfo.BreamLocation.GetEndPoint(0));
				}
				else
				{
					connectColumnBreamInfo.m_intConnectPoint = 1;
					connectColumnBreamInfo.m_arTagLine = ConnectColumnBreamInfo.GettagLine(breamInfo, breamInfo.BreamLocation.GetEndPoint(1));
				}
				connectColumnBreamInfo.m_nextConnect = null;
				connectColumnBreamInfo.m_pConnect = point;
				connectColumnBreamInfo.m_BreamInfo = breamInfo;
				connectColumnBreamInfo.m_column = colum;
				connectColumnBreamInfo.m_arModelLine = ConnectColumnBreamInfo.CreateDetailCurve(connectColumnBreamInfo.m_arTagLine, m_commandData, connectColumnBreamInfo.m_BreamInfo);
				if (connectColumnBreamInfo.m_arModelLine != null)
				{
					result = connectColumnBreamInfo;
				}
				else
				{
					result = null;
				}
			}
			catch
			{
				result = null;
			}
			return result;
		}

		public static List<ConnectColumnBreamInfo> CreateConnectColumnBreamInfos(ExternalCommandData m_commandData, BreamInfo breamInfo, XYZ onePoint, XYZ twoPoint, FamilyInstance oneColum, FamilyInstance twoColum)
		{
			if (m_commandData == null || breamInfo == null || onePoint == null || twoPoint == null)
			{
				return null;
			}
			List<ConnectColumnBreamInfo> list = new List<ConnectColumnBreamInfo>();
			ConnectColumnBreamInfo connectColumnBreamInfo = new ConnectColumnBreamInfo();
			ConnectColumnBreamInfo connectColumnBreamInfo2 = new ConnectColumnBreamInfo();
			connectColumnBreamInfo.m_intConnectPoint = 0;
			connectColumnBreamInfo.m_nextConnect = connectColumnBreamInfo2;
			connectColumnBreamInfo.m_BreamInfo = breamInfo;
			connectColumnBreamInfo2.m_intConnectPoint = 1;
			connectColumnBreamInfo2.m_nextConnect = connectColumnBreamInfo;
			connectColumnBreamInfo2.m_BreamInfo = breamInfo;
			if (breamInfo.BreamLocation.GetEndPoint(0).DistanceTo(onePoint) < breamInfo.BreamLocation.GetEndPoint(0).DistanceTo(twoPoint))
			{
				connectColumnBreamInfo.m_pConnect = onePoint;
				connectColumnBreamInfo.m_arTagLine = ConnectColumnBreamInfo.GettagLine(breamInfo, breamInfo.BreamLocation.GetEndPoint(0));
				connectColumnBreamInfo2.m_pConnect = twoPoint;
				connectColumnBreamInfo2.m_arTagLine = ConnectColumnBreamInfo.GettagLine(breamInfo, breamInfo.BreamLocation.GetEndPoint(0));
			}
			else
			{
				connectColumnBreamInfo.m_pConnect = twoPoint;
				connectColumnBreamInfo.m_arTagLine = ConnectColumnBreamInfo.GettagLine(breamInfo, breamInfo.BreamLocation.GetEndPoint(1));
				connectColumnBreamInfo2.m_pConnect = onePoint;
				connectColumnBreamInfo2.m_arTagLine = ConnectColumnBreamInfo.GettagLine(breamInfo, breamInfo.BreamLocation.GetEndPoint(1));
			}
			list.Add(connectColumnBreamInfo);
			list.Add(connectColumnBreamInfo2);
			return list;
		}

		private static Line[] GettagLine(BreamInfo breamInfo, XYZ point)
		{
			Line[] array = new Line[4];
			XYZ xyz = new XYZ(BreamConnectColumnGeometryToos.OneMillimeter * 500.0, BreamConnectColumnGeometryToos.OneMillimeter * 500.0, breamInfo.BreamLevel.Elevation);
			XYZ xyz2 = new XYZ(-BreamConnectColumnGeometryToos.OneMillimeter * 500.0, BreamConnectColumnGeometryToos.OneMillimeter * 500.0, breamInfo.BreamLevel.Elevation);
			XYZ xyz3 = new XYZ(-BreamConnectColumnGeometryToos.OneMillimeter * 500.0, -BreamConnectColumnGeometryToos.OneMillimeter * 500.0, breamInfo.BreamLevel.Elevation);
			XYZ xyz4 = new XYZ(BreamConnectColumnGeometryToos.OneMillimeter * 500.0, -BreamConnectColumnGeometryToos.OneMillimeter * 500.0, breamInfo.BreamLevel.Elevation);
			xyz += point;
			xyz2 += point;
			xyz3 += point;
			xyz4 += point;
			array[0] = Line.CreateBound(xyz, xyz2);
			array[1] = Line.CreateBound(xyz2, xyz3);
			array[2] = Line.CreateBound(xyz3, xyz4);
			array[3] = Line.CreateBound(xyz4, xyz);
			return array;
		}

		public static DetailCurve[] CreateDetailCurve(Line[] arrLine, ExternalCommandData commandData, BreamInfo breamInfo)
		{
			Document document = commandData.Application.ActiveUIDocument.Document;
			DetailCurve[] array = new DetailCurve[4];
			Transaction transaction = new Transaction(document, "CreateModelLines");
			DetailCurve[] result;
			try
			{
				transaction.Start();
				for (int i = 0; i < arrLine.Length; i++)
				{
					DetailCurve detailCurve = document.Create.NewDetailCurve(breamInfo.BreamViewPlan, arrLine[i]);
					array[i] = detailCurve;
					Color color = new Color(byte.MaxValue, 0, 0);
					List<ElementId> list = new List<ElementId>();
					list.Add(detailCurve.Id);
					breamInfo.BreamViewPlan.SetYJKProjColorOverrideByElement(list, color);
				}
				transaction.Commit();
				result = array;
			}
			catch
			{
				transaction.RollBack();
				result = null;
			}
			return result;
		}

		public void CreateDetailCurve()
		{
			Document document = this.m_BreamInfo.ThisExternalCommandData.Application.ActiveUIDocument.Document;
			DetailCurve[] array = new DetailCurve[4];
			Transaction transaction = new Transaction(document, "CreateModelLines");
			try
			{
				transaction.Start();
				for (int i = 0; i < this.m_arTagLine.Length; i++)
				{
					DetailCurve detailCurve = document.Create.NewDetailCurve(this.m_BreamInfo.BreamViewPlan, this.m_arTagLine[i]);
					array[i] = detailCurve;
				}
				transaction.Commit();
				this.m_arModelLine = array;
			}
			catch
			{
				transaction.RollBack();
			}
		}

		public void DeleteDetailCurve()
		{
			Document document = this.m_BreamInfo.ThisExternalCommandData.Application.ActiveUIDocument.Document;
			DetailCurve[] array = new DetailCurve[4];
			if (this.ArrayDetailCurve == null)
			{
				Transaction transaction = new Transaction(document, "DeleteDetailCurve");
				try
				{
					transaction.Start();
					for (int i = 0; i < this.m_arTagLine.Length; i++)
					{
						document.Delete(this.m_arModelLine[i].Id);
					}
					transaction.Commit();
					this.m_arModelLine = null;
					return;
				}
				catch
				{
					transaction.RollBack();
					return;
				}
			}
			Transaction transaction2 = new Transaction(document, "CreateModelLines");
			try
			{
				transaction2.Start();
				for (int j = 0; j < this.m_arTagLine.Length; j++)
				{
					DetailCurve detailCurve = document.Create.NewDetailCurve(this.m_BreamInfo.BreamViewPlan, this.m_arTagLine[j]);
					array[j] = detailCurve;
				}
				transaction2.Commit();
				this.m_arModelLine = array;
			}
			catch
			{
				transaction2.RollBack();
			}
		}

		private ConnectColumnBreamInfo m_nextConnect;

		private int m_intConnectPoint;

		private XYZ m_pConnect;

		private BreamInfo m_BreamInfo;

		private Line[] m_arTagLine = new Line[4];

		private DetailCurve[] m_arModelLine = new DetailCurve[4];

		private FamilyInstance m_column;
	}
}
